package org.sao.security.demo.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


@Component
public class TimeInterceptor implements HandlerInterceptor{

	
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("afterCompletion");
		Long endTime=(Long) request.getAttribute("startTime");
		System.out.println(new Date().getTime()-endTime);
		System.out.println("ex:"+ex);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("postHandle");
		Long endTime=(Long) request.getAttribute("startTime");
		System.out.println(new Date().getTime()-endTime);
	}
	
	

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("preHandle");
		System.out.println(((HandlerMethod)handle).getBean().getClass().getName());
		System.out.println(((HandlerMethod)handle).getMethod().getName());
		request.setAttribute("startTime", new Date().getTime());
		return true;
	}
	
	

}
